package org.itboys.ace.web.lasted;

import org.itboys.admin.dto.lasted.SystemUserDto;
import org.itboys.admin.entity.lasted.SystemUser;
import org.itboys.admin.service.lasted.SystemUserService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *作者：江雄
 * 描述：系统用户
 * 时间：2017年2月24日.重新构建
 */
@RestController
@RequestMapping("/ace/system/user")
public class AceSystemUserController extends BaseController {
    @Autowired
    private SystemUserService userService;

    /**
     * 主页，即列表页
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/user-list")
    public ModelAndView user_list(HttpServletRequest request, HttpServletResponse response, Model model) {
        return new ModelAndView("/ace/sys/user/user-list");
    }

    /**
     * 详情页
     *
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public ModelAndView user_list(HttpServletRequest request,
                                  HttpServletResponse response, @RequestParam(value = "id", required = true) Long id,
                                  Model model) {
        model.addAttribute("user", userService.findOne(id));
        return new ModelAndView("/ace/sys/user/input");
    }

    /**
     * 分页查询用户
     *
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request, HttpServletResponse response) {

        PageResult<SystemUserDto> result = userService.selectUser(request);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);

    }

    /**
     * 保存
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void save(@ModelAttribute SystemUser user,
                     @RequestParam(value="logoFile",required=false) MultipartFile logoFile,
                     HttpServletRequest request, HttpServletResponse response) {
        try {
            int resultCode = userService.saveUser(user,logoFile);
            if (resultCode == -2) {
                AjaxUtils.renderText(response, "登陆账号已经存在！");
            }
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            logger.error("SystemUser save error", e);
            e.printStackTrace();
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    /**
     * 删除
     *
     * @param id
     * @param response
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public void delete(@RequestParam(value = "id", required = true) Long id, HttpServletResponse response) {
        try {
             userService.delete(id);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            logger.error("SystemUser delete error", e);
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }
    /**
     * 修改密码
     * @param id
     * @param response
     */
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public void updatePassword(@RequestParam(value = "id", required = true) Long id,
                               @RequestParam(value = "oldPassword", required = true) String oldPassword,
                               @RequestParam(value = "newPassword", required = true) String newPassword,
                               HttpServletResponse response) {
        try {
            Integer resultCode = userService.updatePassword(id,oldPassword,newPassword);
            if(resultCode==-2){
                AjaxUtils.renderText(response, "旧密码错误");
            }
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            logger.error("SystemUser delete error", e);
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }

    /**
     * 修改个人信息
     * @param id
     * @param response
     */
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public void updateUser(@RequestParam(value = "id", required = true) Long id,
                           @RequestParam(value = "name", required = false) String name,
                           @RequestParam(value = "mobile", required = false) String mobile,
                           @RequestParam(value = "email", required = false) String email,

                           HttpServletResponse response) {
        try {
            userService.updateUser(id,name, mobile, email);
            AjaxUtils.renderText(response, CommonConstants.SUCCESS);
        } catch (Exception e) {
            logger.error("SystemUser delete error", e);
            AjaxUtils.renderText(response, CommonConstants.FAIL);
        }
    }
    /**
     *
     * --------------用户选择器-------------
     * @param request
     * @param response
     */
    @RequestMapping("/choose")
    public ModelAndView choose(HttpServletRequest request,
                         HttpServletResponse response,
                         @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                         @RequestParam(value="param",required=false)String param,//查询参数
                         @RequestParam(value="callBack",required=false)String callBack,//回调函数
                         Model model
    ) {

        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
       // model.addAttribute("ohterParam",request.getParameter("ohterParam"));
        if(isSingle==null){
            return new ModelAndView("/ace/sys/choose/choose_erro");
        }else if(isSingle==1){
            return new ModelAndView("/ace/sys/user/choose/choose_single");
        }else if(isSingle==0){
            return new ModelAndView("/ace/sys/user/choose/choose_muilt");
        }

        return new ModelAndView("/ace/sys/choose/choose_erro");

    }

}


